import os
import yaml
from utils.data_path_manager import data_path_manager
# from data_path_manager import data_path_manager


class Configurator:
    def __init__(self):
        with open(f"{data_path_manager.project_dir}/config.yml", "r", encoding="utf-8") as f:
            self.config = yaml.safe_load(f)

    # 修改配置文件config.yml
    @classmethod
    def config_edit(cls, token):
        with open(f"{data_path_manager.project_dir}/config.yml", "r", encoding="utf-8") as f:
            # type(result) is "str"
            result = f.read()
            # convert result to "dict"
            x = yaml.load(result, Loader=yaml.FullLoader)

            # change item,if not exist it'll append new item.
            x['account']['token'] = token
            with open(f"{data_path_manager.project_dir}/config.yml", "w", encoding="utf-8") as w_f:
                # overwrite config.yml
                yaml.dump(x, w_f, sort_keys=False)

config: dict = Configurator().config

